<?php
namespace app\admin\controller;
use app\admin\model\AdminRole;
use app\admin\model\AdminUser;
use think\Controller;
use think\facade\Session;

class LoginController extends Controller{
	public function login(){

		return $this->fetch();
	}
	public function doLogin(){
        $username = trim(input('post.username'));
        $password = trim(input('post.password'));
        $user = AdminUser::where('username',$username)->find();
        if (!$user){
            return json(['code'=>0,'msg'=>'用户名不存在']);
        }
        if ($user['status'] == 0){
            return json(['code'=>0,'msg'=>'用户已禁用']);
        }
        $role = AdminRole::where('id',$user['role'])->where('status',1)->find();
        if (!$role){
            return json(['code'=>0,'msg'=>'角色不存在或已禁用']);
        }
        if ($password != $user['password']){
            return json(['code'=>0,'msg'=>'密码错误']);
        }
        Session::set('admin_id',$user['id']);
        Session::set('shop_id',$user['shop_id']);
        return json(['code'=>1,'msg'=>'登录成功']);
    }
	public function loginOut(){
		Session::delete('admin_id');
		Session::delete('shop_id');
        return json(['code'=>1,'msg'=>'退出成功']);
	}
}